#include <iostream>
#include <cstring>
#include <new>

struct chaff
{
    char dross[20];
    int slag;
};

const int bf_len = 1000;
char buff_1[bf_len];

void show_chaff(const chaff & e)
{
    std::cout << &e << " " << e.dross << " - " << e.slag << std::endl;
}

/**
 * 缓冲区
 */
int main(int argc, char const *argv[])
{
    using std::cout;
    using std::endl;
    cout << "静态数组作缓冲区" << endl;
    cout << (void *)buff_1 << "buff_1" << endl;
    chaff * p1 = new(buff_1) chaff[2];
    strcpy(p1[0].dross, "dross_1");
    p1[0].slag = 1;
    strcpy(p1[1].dross, "dross_2");
    p1[1].slag = 2;
    for (int i = 0; i < 2; i++)
    {
        show_chaff(p1[i]);
    }

    cout << "常规new分配缓冲区" << endl;
    char * buff_2 = new char[bf_len];
    cout << (void *) buff_2 << endl;

    chaff * p2 = new(buff_2) chaff[2];
    strcpy(p2[0].dross, "dross_1");
    p2[0].slag = 1;
    strcpy(p2[1].dross, "dross_2");
    p2[1].slag = 2;
    for (int i = 0; i < 2; i++)
    {
        show_chaff(p2[i]);
    }

    return 0;
}
